//1.引入第三方模块
const express = require('express');
//2.需要用到mysql，要引入连接池模块
const pool = require('../pool.js');
//3.创建路由对象
const router = express.Router();
//以下为处理逻辑

//1 user register
//methods: post
//url: /user/reg

router.post('/reg', (req, res, next) => {
  let obj = req.body;
  console.log(obj);

  pool.query('insert into userinfo set ?', [obj], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    console.log(data.affectedRows);

    if(data.affectedRows == 0){
      res.send({
        "code":1,
        "msg":"注册失败"
      })
    }else{
      res.send({
        "code":1,
        "msg":"注册成功"
      })
    }

  })
})


//2.updata msg
//url:  /user/edit
//methods: PUT

router.put('/edit', (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  console.log(obj.uname);

  pool.query(`update userinfo set u_names="${obj.uname}",u_phone="${obj.uphone}",u_member="${obj.umember}" where u_phone="${obj.oldphone}";`, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    console.log(data.affectedRows);

    if(data.affectedRows == 0){
      res.send({
        "code":0,
        "msg":"修改失败"
      })
    }else{
      res.send({
        "code":1,
        "msg":"修改成功"
      })
    }

  })
})




//最后一步，导出（暴露）路由对象
module.exports = router;